package com.mud.mudcommune.view.fragment.twoLevel;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AlphabetIndexer;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mud.mudcommune.R;
import com.mud.mudcommune.adapter.MyAdapter;
import com.mud.mudcommune.view.fragment.HomePagerFragment;
import com.mud.mudcommune.view.widget.MyIndexBar;
import com.mud.mudcommune.view.widget.person_item;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Nullable;

/**
 * A simple {@link Fragment} subclass.
 */
public class ContactFragment extends Fragment implements MyIndexBar.MyindexOnclickListener, AdapterView.OnItemClickListener {
    private ListView listView;
    private List<person_item> list;
    private MyIndexBar myIndexBar;
    private TextView current;
    private int lastFirstVisibleItem = -1;
    private AlphabetIndexer indexer;
    private RelativeLayout head_view;
    private static ContactFragment contacts = new ContactFragment();

    public ContactFragment() {
    }

    public static Fragment getInstance() {
        return contacts;
    }

    private void initData(View view) {
        listView = (ListView) view.findViewById(R.id.contact_listview);
        myIndexBar = (MyIndexBar) view.findViewById(R.id.view);
        head_view = (RelativeLayout) LayoutInflater.from(getContext()).inflate(R.layout.map_header,null);
        current = (TextView) view.findViewById(R.id.current);
        list = new ArrayList<>();
        list.add(new person_item("北京"));
        list.add(new person_item("上海"));
        list.add(new person_item("广州"));
        list.add(new person_item("郴州"));
        list.add(new person_item("长沙"));
        list.add(new person_item("成都"));
        list.add(new person_item("杭州"));
        list.add(new person_item("衡阳"));
        list.add(new person_item("醴陵"));
        list.add(new person_item("南京"));
        list.add(new person_item("深圳"));
        list.add(new person_item("苏州"));
        list.add(new person_item("武汉"));
        list.add(new person_item("株洲"));//长沙会归并到Z里面去
        Collections.sort(list);
        listView.addHeaderView(head_view);
        MyAdapter adapter = new MyAdapter(list, getContext());
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
        myIndexBar.setAnyway(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_contact, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData(view);
    }

    @Override
    public void taoge(String words) {//实现接口回调  返回触摸的字母进行比对
        for (int i = 0; i < list.size(); i++) {
            person_item person_item = list.get(i);
            if (words.equals(person_item.getPinyin().charAt(0) + "")) {
                listView.setSelection(i);//设置左边listview的一个位置
                current.setVisibility(View.VISIBLE);
                current.setText(words);
                handler.sendEmptyMessageDelayed(1, 2000);//延迟两秒自动取消
                return;
            }//跟右边的自定义view进行一个关联
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            current.setVisibility(View.GONE);
        }
    };

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        person_item item = list.get(position-1);
        String cityname = item.getName();
        if(cityname!=null){
            HomePagerFragment.getcity(cityname);
        }
        getFragmentManager().popBackStack();
    }
}
